package com.warom.sdg;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.warom.sdg.model.PlcDataEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * PLC数据列表适配器
 * 用于在ListView中显示PLC数据记录
 */
public class PlcDataAdapter extends BaseAdapter {
    
    private Context context;
    private List<PlcDataEntity> dataList;
    private LayoutInflater inflater;
    private SimpleDateFormat dateFormat;
    
    /**
     * 构造函数
     * @param context 上下文
     * @param dataList 数据列表
     */
    public PlcDataAdapter(Context context, List<PlcDataEntity> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.inflater = LayoutInflater.from(context);
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }
    
    @Override
    public int getCount() {
        return dataList.size();
    }
    
    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_plc_data, parent, false);
            
            holder = new ViewHolder();
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvAddress = convertView.findViewById(R.id.tv_address);
            holder.tvValue = convertView.findViewById(R.id.tv_value);
            holder.tvType = convertView.findViewById(R.id.tv_type);
            holder.tvTimestamp = convertView.findViewById(R.id.tv_timestamp);
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        // 获取当前项的数据 
        PlcDataEntity data = dataList.get(position);
        
        // 设置数据到视图
        holder.tvName.setText(data.getName());
        holder.tvAddress.setText(data.getAddress());
        
        // 显示数值和单位（如果有单位）
        String unitStr = data.getUnit();
        if (unitStr != null && !unitStr.isEmpty()) {
            holder.tvValue.setText(String.format("数值: %s %s", data.getValue(), unitStr));
        } else {
            holder.tvValue.setText(String.format("数值: %s", data.getValue()));
        }
        
        holder.tvType.setText(data.getType());
        
        // 格式化时间戳
        String formattedDate = dateFormat.format(new Date(data.getTimestamp()));
        holder.tvTimestamp.setText(formattedDate);
        
        return convertView;
    }
    
    /**
     * 视图持有者类
     * 用于缓存控件引用，提高性能
     */
    private static class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        TextView tvValue;
        TextView tvType;
        TextView tvTimestamp;
    }
} 